﻿using DevExpress.Utils.Frames;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraWaitForm;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ZMotion
{
    public partial class FormSetting : DevExpress.XtraEditors.XtraForm
    {
       
        ZMotionCard m_card;
        List<AxisStatus>   AxisStatuses;
        List<InputStatus> InputStatuses;
        List<OutputStatus> OutputStatuses;
        public FormSetting()
        {
            InitializeComponent();
        }
        public FormSetting(ZMotionCard zMotionCard)
        {
            InitializeComponent();
            m_card = zMotionCard;
            
            
        }

        private void FormSetting_Load(object sender, EventArgs e)
        {
            //waitForm1 = new WaitForm1();
            //waitForm1.Show(this);
            
            labelControlCardName.Text = m_card.hardwareName;
            propertyGridControl1.SelectedObject = m_card.cardSetting;
           
            memoEditCardInforMation.Text = m_card.GetInfo();
            timerStatusScan.Enabled = true; 
        }
        private void InitMotionGridView()
        {
            AxisStatuses = new List<AxisStatus>();
            for (int i = 0; i < 8; i++)
            {
                AxisStatus axisStatus = new AxisStatus();
                axisStatus.AxisNum = "Axis" + i.ToString("00");
                AxisStatuses.Add(axisStatus);

            }
            gridControlMotion.DataSource = AxisStatuses;

            RepositoryItemButtonEdit edit = new RepositoryItemButtonEdit();
            GridView gridView = gridControlMotion.MainView as GridView;
            gridView.Columns["CCW"].ColumnEdit = edit;
            gridView.Columns["HOME"].ColumnEdit = edit;
            gridView.Columns["CW"].ColumnEdit = edit;
            gridView.Columns["Alarm"].ColumnEdit = edit;
            gridView.Columns["MoveDone"].ColumnEdit = edit;
            gridView.RowCellStyle += (sender1, e1) => {
                GridView view = sender1 as GridView;

                if (e1.Column.FieldName == "CCW")
                {
                    bool _mark = (bool)view.GetRowCellValue(e1.RowHandle, "CCW");
                    e1.Appearance.BackColor = _mark ? Color.LightGreen : Color.LightSalmon;
                }
                if (e1.Column.FieldName == "HOME")
                {
                    bool _mark = (bool)view.GetRowCellValue(e1.RowHandle, "HOME");
                    e1.Appearance.BackColor = _mark ? Color.LightGreen : Color.LightSalmon;
                }
                if (e1.Column.FieldName == "CW")
                {
                    bool _mark = (bool)view.GetRowCellValue(e1.RowHandle, "CW");
                    e1.Appearance.BackColor = _mark ? Color.LightGreen : Color.LightSalmon;
                }
                if (e1.Column.FieldName == "Alarm")
                {
                    bool _mark = (bool)view.GetRowCellValue(e1.RowHandle, "Alarm");
                    e1.Appearance.BackColor = _mark ? Color.LightGreen : Color.LightSalmon;
                }
                if (e1.Column.FieldName == "MoveDone")
                {
                    bool _mark = (bool)view.GetRowCellValue(e1.RowHandle, "MoveDone");
                    e1.Appearance.BackColor = _mark ? Color.LightGreen : Color.LightSalmon;
                }
            };
            AddNewColForMotion(gridView, "JOG+");
            AddNewColForMotion(gridView, "JOG-");
            AddNewColForMotion(gridView, "Rel");
            AddNewColForMotion(gridView, "Abs");
            AddNewColForMotion(gridView, "Org");
            AddNewColForMotion(gridView, "Stop");
        }
        public void AddNewColForMotion(GridView gridView,string strColName)
        {
            RepositoryItemButtonEdit repositoryItemButton = new RepositoryItemButtonEdit();
            repositoryItemButton.ButtonsStyle = DevExpress.XtraEditors.Controls.BorderStyles.Flat;
            repositoryItemButton.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.Standard;
            repositoryItemButton.Buttons[0].Kind = DevExpress.XtraEditors.Controls.ButtonPredefines.Glyph;
            
            repositoryItemButton.Buttons[0].Caption = strColName;
            repositoryItemButton.TextEditStyle = TextEditStyles.HideTextEditor;
            gridView.Columns.AddVisible(strColName);

            gridView.Columns[strColName].ColumnEdit = repositoryItemButton;

            repositoryItemButton.Buttons[0].Click += MotionGridCtrolButton_Click; ;
        }

        private void MotionGridCtrolButton_Click(object sender, EventArgs e)
        {
            EditorButton editorButton = sender as EditorButton;
            GridView gridView = gridControlMotion.MainView as GridView;
            int iIndex = gridView.GetFocusedDataSourceRowIndex();
            if (iIndex > -1)
            {
                if (editorButton.Caption == "JOG+")
                {
                    
                }
                if (editorButton.Caption == "JOG-")
                {

                }
                if (editorButton.Caption == "Rel")
                {

                }
                if (editorButton.Caption == "Abs")
                {

                }
                if (editorButton.Caption == "Org")
                {

                }
                if (editorButton.Caption == "Stop")
                {

                }
                XtraMessageBox.Show("Row:" + iIndex.ToString() + "Click--" + "Action:" + editorButton.Caption);
            }
            
            
        }

        private void InitInputnGridView()
        {
            InputStatuses = new List<InputStatus>();
            for (int i = 0; i < 32; i++)
            {
                InputStatus inputStatus  = new InputStatus();
                inputStatus.InputNum = "Input " + i.ToString("00");
                InputStatuses.Add(inputStatus);

            }
            gridControlInput.DataSource = InputStatuses;

            RepositoryItemButtonEdit edit = new RepositoryItemButtonEdit();
            GridView gridView = gridControlInput.MainView as GridView;
            gridView.Columns["Status"].ColumnEdit = edit;
            gridView.RowCellStyle += (sender1, e1) => {
                GridView view = sender1 as GridView;

                if (e1.Column.FieldName == "Status")
                {
                    bool _mark = (bool)view.GetRowCellValue(e1.RowHandle, "Status");
                    e1.Appearance.BackColor = _mark ? Color.LightGreen : Color.LightSalmon;
                }
            };
        }
        private void InitOutputnGridView()
        {
            OutputStatuses = new List<OutputStatus>();
            for (int i = 0; i < 32; i++)
            {
                OutputStatus outputStatus = new OutputStatus();
                outputStatus.OutputNum = "Output " + i.ToString("00");
                OutputStatuses.Add(outputStatus);

            }
            gridControlOutput.DataSource = OutputStatuses;

            RepositoryItemButtonEdit edit = new RepositoryItemButtonEdit();
            GridView gridView = gridControlOutput.MainView as GridView;
            gridView.Columns["Status"].ColumnEdit = edit;
            gridView.RowCellStyle += (sender1, e1) => {
                GridView view = sender1 as GridView;

                if (e1.Column.FieldName == "Status")
                {
                    bool _mark = (bool)view.GetRowCellValue(e1.RowHandle, "Status");
                    e1.Appearance.BackColor = _mark ? Color.LightGreen : Color.LightSalmon;
                }
            };
            AddNewColForOutput(gridView, "ON");
            AddNewColForOutput(gridView, "OFF");
        }
        public void AddNewColForOutput(GridView gridView, string strColName)
        {
            RepositoryItemButtonEdit repositoryItemButton = new RepositoryItemButtonEdit();
            repositoryItemButton.ButtonsStyle = DevExpress.XtraEditors.Controls.BorderStyles.Flat;
            repositoryItemButton.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.Standard;
            repositoryItemButton.Buttons[0].Kind = DevExpress.XtraEditors.Controls.ButtonPredefines.Glyph;

            repositoryItemButton.Buttons[0].Caption = strColName;
            repositoryItemButton.TextEditStyle = TextEditStyles.HideTextEditor;
            gridView.Columns.AddVisible(strColName);

            gridView.Columns[strColName].ColumnEdit = repositoryItemButton;

            repositoryItemButton.Buttons[0].Click += OutputGridCtrolButton_Click; ;
        }

        private void OutputGridCtrolButton_Click(object sender, EventArgs e)
        {
            EditorButton editorButton = sender as EditorButton;
            GridView gridView = gridControlOutput.MainView as GridView;
            int iIndex = gridView.GetFocusedDataSourceRowIndex();
            if (iIndex > -1)
            {
                if (editorButton.Caption == "ON")
                {
                    m_card.SetOutBit(iIndex, true);
                }
                if (editorButton.Caption == "OFF")
                {
                    m_card.SetOutBit(iIndex, false);
                }
                //XtraMessageBox.Show("Row:" + iIndex.ToString() + "Click--" + "Action:" + editorButton.Caption);
            }


        }

        bool bInitMotionGridCtrol = false;
        private void timerStatusScan_Tick(object sender, EventArgs e)
        {
           
            if (this == null)
            {
                return;
            }
            if (this.Visible == false)
            {
                return;
            }
            if (bInitMotionGridCtrol==false)
            {
                bInitMotionGridCtrol = true;
                InitMotionGridView();
                InitInputnGridView();
                InitOutputnGridView();
                //waitForm1.Close();
            }
            //马达刷新
            for (int i = 0; i < 8; i++)
            {
                double iCurrentPos;
                double iCommandPos;
                bool bCCWL = m_card.GetCCWL(i);
                bool bORG = m_card.GetHome(i);
                bool bCWL = m_card.GetCWL(i);
                bool bALM = m_card.GetAlarm(i);
                bool bMD = m_card.MoveDone(i);
                m_card.GetPos(i, out iCurrentPos);
                m_card.GetCmdPos(i, out iCommandPos);
                AxisStatuses[i].CommandPos =iCommandPos;
                AxisStatuses[i].CurentPos = iCurrentPos;
                AxisStatuses[i].CCW = bCCWL;
                AxisStatuses[i].HOME = bORG;
                AxisStatuses[i].CW = bCWL;
                AxisStatuses[i].Alarm = bALM;
                AxisStatuses[i].MoveDone = bMD ;
            }
            for (int i = 0; i < 32; i++)
            {
                
                InputStatuses[i].Status = m_card.GetInput(i);
                OutputStatuses[i].Status = m_card.GetOutBit(i);
            }
            gridControlMotion.RefreshDataSource();
            gridControlInput.RefreshDataSource();
            gridControlOutput.RefreshDataSource();
            ////Input Scan
            //for (int i = 0; i < 128; i++)
            //{
            //    bool bOn = m_card.GetInput(i);
            //    dataGridViewInput.Rows[i].Cells[1].Style.BackColor = bOn ? Color.Green : Color.White;
            //}
            ////Output Scan
            //for (int i = 0; i < 128; i++)
            //{
            //    bool bOn = m_card.GetOutBit(i);
            //    dataGridViewOutput.Rows[i].Cells[1].Style.BackColor = bOn ? Color.Green : Color.White;
            //}
            ////AD
            ////for (int i = 0; i < 8; i++)
            ////{
            ////    int iValue = 0;
            ////    m_card.GetADValue(i,out iValue);
            ////    dataGridViewAD.Rows[i].Cells[1].Value = iValue;
            ////}
            ////DA
            ////for (int i = 0; i < 8; i++)
            ////{
            ////    int iValue = 0;
            ////    m_card.GetDAValue(i, out iValue);
            ////    dataGridViewDA.Rows[i].Cells[1].Value = iValue;
            ////}
            //labelScanTime.Text = m_card.dScanTime.ToString("0.0000");
            //labelState.Text = m_card.strCardInfor;
        }
    }
}
